<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Service\BotsService;
use App\Service\steampowered\DologinService;
use App\Model\BotsModel;

class LoginRobotSteampowered extends Command
{
	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'login:robotsteampowered {--bot_id=}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = '登录机器人.';

	protected $botId = 0;

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * 获取所有机器人信息
	 * @param  int $botId 机器人ID
	 * @return array
	 */
	protected function getBotsInfoByAll($botId = NULL)
	{
		return BotsModel::getBotsInfoByAll($botId);
	}

	protected function loginRobot()
	{
		$info = $this->getBotsInfoByAll($this->botId);
		if (!$info) {
			echo 'query bots failed'."\r\n";
			return false;
		}

		foreach ($info as $bots) {
			$setting = BotsService::getInstance()->getBotsSetting($bots);
			// login robot
			$res = DologinService::getInstance()->dologin($setting);
			if (!$res) {
				echo 'login robot failed, botid:'.$bots['bot_id']."\r\n";
				continue;
			}
			echo 'login robot success, botid:'.$bots['bot_id']."\r\n";
			sleep(20);
		}
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		$this->botId = $this->option('bot_id');

		$this->loginRobot();
		echo '数据查询完毕!'."\r\n";
		return true;
	}
}